package com.burst.reptile.framework.web.result;

import com.burst.reptile.common.constant.HttpStatus;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;

/**
 * 通用业务返回结果封装
 * @author wujin
 * @date: 2020年2月5日 下午2:19:46
 */
@AllArgsConstructor
@Builder
@Data
public class Result<T> {
	
	public static final String DEFAULT_SUCCESS_MESSAGE = "success";

	public static final String DEFAULT_ERROR_MESSAGE = "fail";

	public static final Integer DEFAULT_SUCCESS_CODE = 200;

	public static final Integer DEFAULT_ERROR_CODE = 500;

	private int code;

	private String msg;

	private T data;

	/**
	 * 成功时候的调用
	 */
	public static <T> Result<T> success(T data) {
		return new Result<T>(DEFAULT_SUCCESS_MESSAGE, data);
	}

	/**
	 * 失败时候的调用
	 */
	public static <T> Result<T> error(String msg) {
		return new Result<T>(HttpStatus.ERROR, msg);
	}
	
	/**
	 * 失败时候的调用
	 */
	public static <T> Result<T> error(Integer code, String msg) {
		return new Result<T>(code, msg);
	}
	
	/**
	 * 失败时候的调用
	 */
	public static <T> Result<T> error(Integer code, String msg, T data) {
		return new Result<T>(code, msg, data);
	}


	public static <T> Result<T> error() {
		return new Result<T>(DEFAULT_ERROR_CODE, DEFAULT_ERROR_MESSAGE);
	}

	private Result(String msg, T data) {
		this.msg = msg;
		this.data = data;
	}

	public Result(Integer code, String msg) {
		this.code = code;
		this.msg = msg;
	}
	
	public Result(Integer code, String msg, T data) {
		this.code = code;
		this.msg = msg;
		this.data = data;
	}

}
